﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
using System.Text;
using  Object = UnityEngine.Object;

namespace HDJ.Framework.Core
{

    public static class ResourceManager
    {
        
        private static AssetsLoadType loadType = AssetsLoadType.Resources;
        public static AssetsLoadType LoadType
        {
            get
            {
                return loadType;
            }

            set
            {
                ReleaseAll();

                loadType = value;
                isInit = false;
                Initialize();
            }
        }

        private static AssetsLoadController loadAssetsController;

        private static bool isInit = false;
#if UNITY_EDITOR
        [UnityEditor.InitializeOnLoadMethod]
#endif
        private static void Initialize()
        {
            if (isInit)
                return;
            loadAssetsController = new AssetsLoadController(loadType);
        }

        public static AssetsLoadController GetLoadAssetsController()
        {
            return loadAssetsController;
        }

        /// <summary>
        /// 同步加载一个资源
        /// </summary>
        /// <param name="name"></param>
        /// <param name="callBack"></param>
        public static Object Load(string name)
        {
            name = name.ToLower();
            AssetsData assets = loadAssetsController.LoadAssets(name);
            if (assets != null)
            {
                return assets.Assets[0];
            }
            return null;
        }

        public static void LoadAsync(string name, CallBack<Object> callBack)
        {
            name = name.ToLower();
            loadAssetsController.LoadAsync(name, null, callBack);
        }
        public static void LoadAsync(string name, Type resType, CallBack<Object> callBack)
        {
            name = name.ToLower();
            loadAssetsController.LoadAsync(name, resType, callBack);
        }
        public static T Load<T>(string name) where T : Object
        {
            T res = null;
            name = name.ToLower();
            AssetsData assets = loadAssetsController.LoadAssets<T>(name);
            if (assets != null)
            {
                res = assets.GetAssets<T>();

            }
            if (res == null)
            {
                Debug.LogError("Error=> Load Name :" + name + "  Type:" + typeof(T).FullName + "\n" + " Load Object:" + res);
            }
            return res;
        }
        public static string LoadText(string name)
        {
            TextAsset tex = Load<TextAsset>(name);
            if (tex == null)
                return null;
            return tex.text;
        }
        /// <summary>
        /// 资源中是否存在该名字的资源，忽略大小写
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool ExistAssets(string name)
        {
            name = name.ToLower();
            return AssetsPathController.ExistFile(name);
        }
        public static void DestoryAssetsCounter(string name)
        {
            name = name.ToLower();
            loadAssetsController.DestoryAssetsCounter(name);
        }
        public static void ReleaseAll()
        {
            if (loadAssetsController != null)
                loadAssetsController.ReleaseAll();
            AssetsPathController.ClearPathData();
        }
        public static void Release(string name)
        {
            name = name.ToLower();
            loadAssetsController.Release(name);
        }

    }
}
